/*
 * CollisionEditorDialog.java
 *
 * Created on June 23, 2008, 4:40 PM
 */

package levelbuilder;

import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

/**
 *
 * @author  ew562c
 */
public class CollisionEditorDialog extends javax.swing.JDialog {

    Vector<CollisionBox> collisions;
    CollisionBox current_cb;
    Image img;
    boolean drawBox = false;
    boolean dragging = false;
        
    /** Creates new form CollisionEditorDialog */
    public CollisionEditorDialog(java.awt.Frame parent, boolean modal, Image img) {
        super(parent, modal);
        initComponents();
        collisions = new Vector();
        collisions.clear();
        initComponents();
        this.img = img;
        current_cb = new CollisionBox();
        
        imgPanel.addMouseMotionListener(new MouseMotionListener(){
           public void mouseMoved(MouseEvent e)
            {

            }
                    
            public void mouseDragged(MouseEvent e)
            {
                if(dragging)
                {
                      current_cb.w = e.getX() - current_cb.x;
                      current_cb.h = e.getY() - current_cb.y;
                      repaint();
                }
            } 
        });
        
        imgPanel.addMouseListener(new MouseListener(){ 
            public void mouseClicked(MouseEvent e) {
                    
                  }
                    
                    public void mouseEntered(MouseEvent e) {
                  }

                  public void mouseExited(MouseEvent e) {
                  }

                  public void mousePressed(MouseEvent e) { 
                    if(!dragging)
                    {
                        current_cb.x = (double)e.getX();
                        current_cb.y = (double)e.getY();
                        dragging = true;
                    }
                    
                  }

                  public void mouseReleased(MouseEvent e) {
                      if(dragging)
                      {
                          dragging = false;
                          current_cb.w = e.getX() - current_cb.x;
                          current_cb.h = e.getY() - current_cb.y;
                          AddBox(current_cb.x, current_cb.y, current_cb.w, current_cb.h);
                          repaint();
                      }
                  }
            
        });
        repaint();
    }
    public void SetCollisions(Vector cv){collisions = cv;}
    public Vector GetCollisions(){return collisions;}
    
    public void AddBox(double x, double y, double w, double h)
    {
        CollisionBox cb = new CollisionBox();
        cb.x = (x*img.getWidth(null))/imgPanel.getWidth();
        cb.y = (y*img.getHeight(null))/imgPanel.getHeight();
        cb.w = (w*cb.x)/x;
        cb.h = (h*cb.y)/y;
        collisions.add(cb);
    }
    
    public int GetCollisionBoxCount(){return collisions.size();}
    
    public CollisionBox GetBox(int i){return collisions.elementAt(i);}

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        imgPanel = new javax.swing.JPanel();
        resetButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        imgPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout imgPanelLayout = new javax.swing.GroupLayout(imgPanel);
        imgPanel.setLayout(imgPanelLayout);
        imgPanelLayout.setHorizontalGroup(
            imgPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 378, Short.MAX_VALUE)
        );
        imgPanelLayout.setVerticalGroup(
            imgPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 302, Short.MAX_VALUE)
        );

        resetButton.setText("Reset");
        resetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetButtonActionPerformed(evt);
            }
        });

        okButton.setText("Ok");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(imgPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(resetButton, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 196, Short.MAX_VALUE)
                        .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(imgPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(resetButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetButtonActionPerformed
// TODO add your handling code here:
        collisions.clear();
        repaint();
}//GEN-LAST:event_resetButtonActionPerformed

private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
// TODO add your handling code here:
    this.setVisible(false);
}//GEN-LAST:event_okButtonActionPerformed

public void paint(Graphics g)
{
    super.paint(g);
    imgPanel.getGraphics().drawImage(this.img,0,0,imgPanel.getWidth(), imgPanel.getHeight(), Color.BLACK, null);

    for(int i = 0; i < collisions.size(); i++)
    {
        CollisionBox cb = collisions.elementAt(i);
        Graphics g2 = imgPanel.getGraphics();
        g2.setColor(Color.YELLOW);
        CollisionBox tmp_box = new CollisionBox();
        tmp_box.x = (cb.x*imgPanel.getWidth())/img.getWidth(null);
        tmp_box.y = (cb.y*imgPanel.getHeight())/img.getHeight(null);
        tmp_box.w = (cb.w*tmp_box.x)/cb.x;
        tmp_box.h = (cb.h*tmp_box.y)/cb.y;
        g2.drawRect((int)tmp_box.x,(int)tmp_box.y,(int)tmp_box.w,(int)tmp_box.h);
    }
    
    if(dragging)
    {
        Graphics g2 = imgPanel.getGraphics();
        g2.setColor(Color.YELLOW);
        g2.drawRect((int)current_cb.x, (int)current_cb.y, (int)current_cb.w, (int)current_cb.h);
    }
}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel imgPanel;
    private javax.swing.JButton okButton;
    private javax.swing.JButton resetButton;
    // End of variables declaration//GEN-END:variables

}
